from pydantic import BaseModel
import time
import hashlib
import base64
class Cookie(BaseModel):
    username: str
    role:str
    sign:str
    timestr:str
class Cookievalue:
    def __init__(self,username,role) -> None:
        self.timestr=str(time.time())
        self.test=username+role+self.timestr
       
        self.sign=hashlib.md5(self.test.encode('utf-8')).hexdigest()
        self.timestr=base64.b64encode(self.timestr.encode('utf-8')).decode('utf-8')
       
        self.cookie=Cookie(username=username,role=role,sign=self.sign,timestr=self.timestr)
    def cookieValueRex(self,data: dict):
        cookieList=['username', 'role', 'sign', 'timestr']
        try:
            for i in cookieList:
                data[i]
        except KeyError:
            return {'code':400}
            
        timestr=base64.b64decode(data['timestr']).decode('utf-8')
     
        test=data['username']+data['role']+timestr
        
        sign=hashlib.md5(test.encode('utf-8')).hexdigest()
        if data['sign']!=sign:
            return {'code':400}
        return {"code":200}
